home *** CD-ROM | disk | FTP | other *** search
- Path: tech.cftnet.com!not-for-mail
- From: wcowley@cftnet.com (Wes Cowley)
- Newsgroups: comp.lang.c++
- Subject: Re: C++ OO question (long)
- Date: 28 Feb 1996 10:48:28 GMT
- Organization: CFTnet
- Message-ID: <4h1bts$45u@tech.cftnet.com>
- References: <4h08uq$mve@madeline.INS.CWRU.Edu>
- NNTP-Posting-Host: ppp244_8.cftnet.com
- X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
-
- Bryan Murphy (bf461@cleveland.Freenet.Edu) wrote:
-
- : I can't explain, for instance, this works:
- :
- : Matrix M3 = Matrix1 * Matrix2;
- :
- : whereas defining Matrix M3 ahead of time
- :
- : M3 = Matrix1 * Matrix2;
- :
- : crashes out on me giving me an error. I can't really tell why.
- : It's a memory allocation assertion error. I can't figure out
- : why I can't do the second method. I was thinking about this,
- : and I have another question related to this.
-
- : When I define the Matrix M3 and assign it right away, is the
- : Default Blank Constructor called, THEN the Copy Constructor?
-
- Yes. Without a fairly heavy optimization (over multiple compilation units
- in general) it's not possible to tell whether or not there is code in the
- constructor that has to be executed before the assignment operator. Both
- of them have to be executed.
-
- But let's back up a second: you talked about the "default blank constructor"
- and the "copy constructor". That's not exactly what's going on here. The
- default blank constructor isn't called at all and there are two steps
- missing at the end.
- 1. The void constructor you created, not the default one, is called
- to create M3.
- 2. operator* creates a new Matrix on the stack and returns it.
- 3. The copy constructor is called to copy that Matrix to a temporary
- to hold the return value. Your copy constructor allocates a
- new array to hold the results and copies them cell by cell.
- Just fine so far.
- 4. Here's the first missing step: you haven't defined an operator=
- so the default operator= is called to assign the temporary to M3.
- This is where your problem is most likely. The default operator=
- does a memberwise assignment from the temporary to M3. That's fine
- for xdim and ydim, they're just ints. But MatrixData is a pointer
- that was allocated with new. Both M3 and the temporary now have
- the same pointer.
- 5. Finally, the destructor is called to cleanup the temporary. It
- deletes MatrixData. Oops: M3 has a pointer to a deallocated chunk
- of memory.
-
-
- : Also, say the copy Constructor was called. When the new data is copied
- : over, is the Destructor called before the copy constructor is used to copy
- : the new data over?
-
- I think you're confusing the copy constructor with operator=. But in both
- cases the answer is no. The copy constructor is building a new object:
- there's nothing to call the destructor on because nothing has been
- constructed yet. operator= doesn't call the destructor either: you're
- not destroying the object, you're assigning a new value to it. Sometimes
- you'll need to clean up part of the object (deallocate some pointers before
- you assign new values to them, etc) but you have to do that yourself.
-
-
- : Also, my second problem is pretty simple. It relates to using
- : variable arguments. Is there anything peculiar or different
- : about Visual C++'s variable arguments?
-
- I don't see where you're using variable arguments (using ... in a functino
- prototype), so I'm assuming you mean the default parameter in your
- constructor. I doubt VC++ handles those any differently than any other
- compiler.
-
-
- Wes
-